<?php
namespace frontend\controllers;

use Yii;
use yii\base\InvalidParamException;
use yii\web\BadRequestHttpException;
use yii\web\Controller;
use frontend\models\SignupForm;
use common\models\LoginForm;
use yii\web\UploadedFile;

	class UserController extends Controller
	{  
		public $layout="ll.php";

      public function actions()
     {
        return [
            'error' => [
                'class' => 'yii\web\ErrorAction',
            ],
            'captcha' => [
                'class' => 'yii\captcha\CaptchaAction',
                 'maxLength' => 4,
                 'minLength' => 4,
                'fixedVerifyCode' => YII_ENV_TEST ? 'testme' : null,
            ],
        ];
      }

       public function actionLogin()
       {   
       	    $model = new LoginForm();
	        if ($model->load(Yii::$app->request->post()) && $model->login()) {
	         
	             return $this->goHome();
	        } else {
	            return $this->render('login', [
	                'model' => $model,
	            ]);
	        }

       }



       function actionReg()
       { 
       	 $model = new SignupForm();
       	 if ($model->load(Yii::$app->request->post())) {
           
           //上传图片
           $model->file = UploadedFile::getInstance($model, 'file');
           $img_name = '../uploads/' . date("Ymd").rand(10,99) . '.' . $model->file->extension;
           $model->file->saveAs($img_name); 
            
           
            if ($user = $model->signup($img_name)) {
              
                if (Yii::$app->getUser()->login($user)) {
                  
                    return $this->goHome();
                }
            }

        }
  
        return $this->render('reg', [
            'model' => $model,
        ]);
       }


       function actionOut()
       {
          Yii::$app->user->logout();

         return $this->goHome();
       }

       
	}